package com.mobileagri.backoffice.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mobileagri.backoffice.dao.ContactDAO;
import com.mobileagri.backoffice.model.Contact;

/**
 * Contact Service
 * 
 * @author LOUVET Romain
 */
@Service
public class ContactService {

	@Autowired
	private ContactDAO contactDAO;

	/**
	 * Get all contacts
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Contact> getContactList() {

		return contactDAO.getAll();
	}

	/**
	 * Get all contacts
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Contact> getContactList(int start, int limit) {

		return contactDAO.getAll(start, limit);
	}

	/**
	 * Create new Contact/Contacts
	 * 
	 * @param data
	 *            - json data from request
	 * @return created contacts
	 */
	@Transactional
	public List<Contact> create(Contact contact) {

		List<Contact> newContacts = new ArrayList<Contact>();

		newContacts.add((Contact) contactDAO.save(contact));

		return newContacts;
	}

	/**
	 * Update contact/contacts
	 * 
	 * @param data
	 *            - json data from request
	 * @return updated contacts
	 */
	@Transactional
	public List<Contact> update(Contact contact) {

		List<Contact> returnContacts = new ArrayList<Contact>();

		returnContacts.add((Contact) contactDAO.save(contact));

		return returnContacts;
	}

	/**
	 * Delete contact/contacts
	 * 
	 * @param contact
	 *            - json data from request
	 */
	@Transactional
	public void delete(Contact contact) {

		contactDAO.delete(contact.getId());
	}

	/**
	 * Get total of Contacts from database. Need to set this value on ExtJS
	 * Store
	 * 
	 * @return
	 */
	public int getTotalContacts() {

		return contactDAO.getTotal();
	}

	/**
	 * Spring use - DI
	 * 
	 * @param contactDAO
	 */
	@Autowired
	public void setContactDAO(ContactDAO contactDAO) {
		this.contactDAO = contactDAO;
	}

}
